<template>
  <div class="search-result">
    <van-list @load="getResult" v-model="loading" :finished="finished" finished-text="没有更多的结果啦" :error.sync="error"
      error-text="请求失败，点击重新加载">
      <van-cell :title="obj.title" v-for="obj in result" :key="obj.id"></van-cell>
    </van-list>
  </div>
</template>

<script>
import { searchResultAPI } from '@/api'
export default {
  created() {
    this.getResult()
  },
  data() {
    return {
      loading: false,
      page: 1,
      result: [],
      finished: false,
      error: false
    }
  },
  methods: {
    async getResult() {
      try {
        const { data } = await searchResultAPI(this.$parent.keywords, this.page++)
        this.result.push(...data.data.results)
        if (data.data.results.length === 0) {
          this.finished = true
        }
      } catch (error) {
        this.error = true
      } finally {
        this.loading = false
      }
    }
  }
}
</script >

<style lang="less" scoped>
.search-result {
  height: calc(100vh - 108px);
  overflow: auto;
}
</style>